package Dao;

import Model.Money;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class DaoMonHandle {
    private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;
    static StudentDao studentDao;

    public static void start() throws IOException {
        in = Resources.getResourceAsStream("config/mybatis_config.xml");
        factory = new SqlSessionFactoryBuilder().build(in);
        sqlSession = factory.openSession();
        studentDao = sqlSession.getMapper(StudentDao.class);
    }

    public void showMonDAO() throws Exception {
        //配置日志
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
        start();
        List<Money> all = studentDao.findAll();
        System.out.println("----------------零钱通菜单--------------");
        for (int i = 0; i < all.size(); i++) {
            System.out.print(all.get(i).getName());
            System.out.print(all.get(i).getMoney() + "\t\t\t");
            System.out.print(all.get(i).getTime() + "\t\t");
            System.out.print(all.get(i).getBalance());
            System.out.println();
        }
//        List<Object[]> objects = JdbcTools.queryArray1("select * from jingqian");
//        System.out.println("----------------零钱通菜单--------------");
//        for (int i = 0; i < objects.size(); i++) {
//            int len=objects.get(i).length-1;
//            for (int j = 1; j <objects.get(i).length-1 ; j++) {
//                System.out.print("\t"+objects.get(i)[j]+"\t");
//
//            }
//            System.out.print("余额："+objects.get(i)[len]);
//            System.out.println();
//
//        }
    }

    public void addMonDAO() throws Exception {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入你的收支名称");
//        String name = scanner.next();
//        System.out.println("请输入你的收支的金额");
//        Double money = scanner.nextDouble();
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mmmm-dd hh:mm:ss");
//        String format = simpleDateFormat.format(date);
//
//        Double balance1 = (Double) balance.get("balance");
//        balance1+=money;
//        String str = "+" + money;
//        JdbcTools.update("insert into jingqian values (default,?,?,?,?)",name,str,format,balance1);
//
//        Double select = studentDao.select();
        Money money = new Money();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的收支名称");
        String name = scanner.next();
        System.out.println("请输入你的收支的金额");
        Double money1 = scanner.nextDouble();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mmmm-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        start();
        Double balance = studentDao.select();
        System.out.println("balance = " + balance);
        balance+=money1;
        String str = "+" + money1;
        System.out.println("aa");
        money.setMoney(str);
        money.setTime(format);
        money.setName(name);
        money.setBalance(balance);

        studentDao.saveStudent(money);
        sqlSession.commit();

        System.out.println("入账成功");
    }


    public void updateMonDAO() throws Exception {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入你的消费名称");
//        String name = scanner.next();
//        System.out.println("请输入你的消费的金额");
//        Double money = scanner.nextDouble();
//        Date date = new Date();
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mmmm-dd hh:mm:ss");
//        String format = simpleDateFormat.format(date);
//        Map<String, Object> balance = JdbcTools.queryMap1("select balance from jingqian order by id desc limit 1 ");
//        Double balance1 = (Double) balance.get("balance");
//        if(money>balance1){
//            System.out.println("余额不足");
//        }else {
//            balance1 -= money;
//            String str = "-" + money;
//            JdbcTools.add("insert into jingqian values (default,?,?,?,?)", name, str, format, balance1);
//            System.out.println("消费入账成功");
//        }
//    }
        Money money = new Money();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的支出名称");
        String name = scanner.next();
        System.out.println("请输入你的支出的金额");
        Double money1 = scanner.nextDouble();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mmmm-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        start();
        Double balance = studentDao.select();
        System.out.println("balance = " + balance);
        balance-=money1;
        String str = "-" + money1;
        System.out.println("aa");
        money.setMoney(str);
        money.setTime(format);
        money.setName(name);
        money.setBalance(balance);

        studentDao.saveStudent(money);
        sqlSession.commit();

        System.out.println("消费入账成功成功");
    }
}